home *** CD-ROM | disk | FTP | other *** search
/ Champak 52 / Volume 52 - JOGO DISK .iso / Games / skipandgouls.swf / scripts / __Packages / characters / SpongeBob.as < prev    next >
Text File  |  2007-10-01  |  15KB  |  550 lines

  1. class characters.SpongeBob extends sarbakan.visual.element.DynamicElement
  2. {
  3.    static var SPEED_RUN = 8;
  4.    static var SPEED_FALL = 10;
  5.    static var SPEED_SLIDE = 10;
  6.    static var SPEED_SLIDE_GREASED = 16;
  7.    static var SPEED_FALL_MOVEX = 4;
  8.    static var FR_INTUBE = 15;
  9.    static var FR_MASKDESTROY = 8;
  10.    static var FR_SCARED_YELL = 5;
  11.    static var CORRECTION_TUBE = 18;
  12.    static var CORRECTION_ENDSLIDE = 40;
  13.    static var BASH_BASE_RATE = 1;
  14.    static var BASH_BASE_SPEED = [10,8,5];
  15.    static var BASE_CAMERAX = -295;
  16.    static var BASE_CAMERAY = -150;
  17.    static var TIMEOUT_MASK = 500;
  18.    static var TIMEOUT_GREASE = 800;
  19.    function SpongeBob(l_mc)
  20.    {
  21.       super("spongebob");
  22.       this.setMC(l_mc);
  23.       this.enable();
  24.       this.setState("waitForStart");
  25.       this.bAlwaysActive = true;
  26.       this.bStaticMovieClip = true;
  27.       this.oKey = sarbakan.input.KeyManager.getInstance();
  28.       this.oKey.addKeyListener(38,"UP");
  29.       this.oKey.addKeyListener(40,"DOWN");
  30.       this.oKey.addKeyListener(37,"LEFT");
  31.       this.oKey.addKeyListener(39,"RIGHT");
  32.       this.oKey.addKeyListener(32,"SPACE");
  33.       this.oKey.addKeyListener(80,"P");
  34.       this.mc.swapDepths(maze.MazeData.DEPTH_SPONGEBOB);
  35.       this.mc._x = (_global.C.oModel.nWidth * maze.MazeData.VIEW_TILE_WIDTH - this.mc._width) / 2;
  36.       if(!characters.SpongeBob.ORIGINAL_Y)
  37.       {
  38.          characters.SpongeBob.ORIGINAL_Y = this.mc._y;
  39.       }
  40.       else
  41.       {
  42.          this.mc._y = characters.SpongeBob.ORIGINAL_Y;
  43.       }
  44.       this.nCameraX = characters.SpongeBob.BASE_CAMERAX;
  45.       this.nCameraY = characters.SpongeBob.BASE_CAMERAY;
  46.       this.bAutoBash = false;
  47.       this.bLevelRestarting = false;
  48.       this.bCanBeScared = false;
  49.       this.bMasked = false;
  50.       this.bMaskDestroying = false;
  51.       this.nMaskDestroyCount = 0;
  52.       this.bGreased = false;
  53.       this.nTimeout = 0;
  54.    }
  55.    function update()
  56.    {
  57.       super.update();
  58.       this.bCanBeScared = this.canBeScared();
  59.       this.updateMask();
  60.       this.updateGrease();
  61.    }
  62.    function getCoord()
  63.    {
  64.       return {x:this.mc._x + this.nCameraX,y:this.mc._y + this.nCameraY};
  65.    }
  66.    function onScared()
  67.    {
  68.       this.nSpeedX = 0;
  69.       this.setState("scared");
  70.    }
  71.    function setPumpkin(l_mcPumpkin)
  72.    {
  73.       this.mcPumpkin = l_mcPumpkin;
  74.       this.updateMask();
  75.    }
  76.    function setGrease(l_mcGrease)
  77.    {
  78.       this.mcGrease = l_mcGrease;
  79.       this.updateGrease();
  80.    }
  81.    function waitForStart()
  82.    {
  83.    }
  84.    function idle()
  85.    {
  86.       this.nSpeedX = 0;
  87.       this.nSpeedY = 0;
  88.       if(this.oKey.LEFT || this.oKey.RIGHT)
  89.       {
  90.          this.setState("run");
  91.       }
  92.       this.checkPipe();
  93.       this.checkFall();
  94.       this.checkDry();
  95.       this.checkItem();
  96.    }
  97.    function run()
  98.    {
  99.       this.checkWall();
  100.       if(!this.oKey.LEFT && !this.oKey.RIGHT)
  101.       {
  102.          this.setState("idle");
  103.       }
  104.       if(this.oKey.LEFT)
  105.       {
  106.          this.nSpeedX = characters.SpongeBob.SPEED_RUN * -1;
  107.       }
  108.       else if(this.oKey.RIGHT)
  109.       {
  110.          this.nSpeedX = characters.SpongeBob.SPEED_RUN;
  111.       }
  112.       this.mc.state.splat._visible = this.bGreased;
  113.       this.checkDry();
  114.       this.checkItem();
  115.       this.checkFall();
  116.    }
  117.    function fallStart()
  118.    {
  119.       this.fall();
  120.       if(this.stateFinished())
  121.       {
  122.          this.setState("fall");
  123.       }
  124.    }
  125.    function fall()
  126.    {
  127.       this.checkWall();
  128.       this.nSpeedY = characters.SpongeBob.SPEED_FALL;
  129.       if(this.isOnGround())
  130.       {
  131.          this.setPositionOutFloor();
  132.          this.nSpeedY = 0;
  133.          this.setState("land");
  134.       }
  135.       else if(this.oKey.LEFT)
  136.       {
  137.          this.nSpeedX = -1 * characters.SpongeBob.SPEED_FALL_MOVEX;
  138.       }
  139.       else if(this.oKey.RIGHT)
  140.       {
  141.          this.nSpeedX = characters.SpongeBob.SPEED_FALL_MOVEX;
  142.       }
  143.       else
  144.       {
  145.          this.nSpeedX = 0;
  146.       }
  147.    }
  148.    function land()
  149.    {
  150.       if(this.stateFinished())
  151.       {
  152.          this.setState("idle");
  153.       }
  154.    }
  155.    function jumpInPipe()
  156.    {
  157.       if(this.mc.state._currentframe == characters.SpongeBob.FR_INTUBE)
  158.       {
  159.          this.mc._x = this.oSlidingPipe.mc._x + characters.SpongeBob.CORRECTION_TUBE;
  160.       }
  161.       if(this.stateFinished())
  162.       {
  163.          this.startBashingPipe();
  164.          this.setState("getInPipe");
  165.       }
  166.    }
  167.    function getInPipe()
  168.    {
  169.       if(!this.bBashingA)
  170.       {
  171.          this.mc.state.prevFrame();
  172.       }
  173.       else
  174.       {
  175.          this.mc.state.stop();
  176.       }
  177.       this.checkDry();
  178.       if(this.oKey.UP)
  179.       {
  180.          this.endBashingPipe(false);
  181.       }
  182.       if(!sarbakan.sound.SoundManager.isSoundPlaying(this.oSquishSound))
  183.       {
  184.          this.oSquishSound = sarbakan.sound.SoundManager.play("squish" + sarbakan.utils.MoreMath.random(1,6),maze.MazeData.VOLUME_SQUISH,1);
  185.       }
  186.    }
  187.    function slidePipe()
  188.    {
  189.       if(sarbakan.sound.SoundManager.isSoundPlaying(this.oSquishSound))
  190.       {
  191.          sarbakan.sound.SoundManager.fade(this.oSquishSound,sarbakan.sound.SoundManager.FADE_VOLUME,0,10);
  192.       }
  193.       this.nSlideDistance += this.nSpeedY;
  194.       if(this.nSlideDistance >= this.nSlideDistanceTotal)
  195.       {
  196.          this.nSpeedY = 0;
  197.          this.mc._y += characters.SpongeBob.CORRECTION_ENDSLIDE;
  198.          this.setState("outPipeGood");
  199.       }
  200.    }
  201.    function outPipeGood()
  202.    {
  203.       if(this.stateFinished())
  204.       {
  205.          this.setState("fall");
  206.          if(this.oSlidingPipe.bLastPipe)
  207.          {
  208.             if(_global.C.nLevelPart < 3 || _global.C.oUnlockedCodes.data.BONUS_LEVEL && _global.C.nLevelPart < 4)
  209.             {
  210.                _global.C.oWater.setState("done");
  211.                _global.C.endLevelPart();
  212.             }
  213.             else
  214.             {
  215.                _global.C.endGame(true);
  216.             }
  217.          }
  218.       }
  219.    }
  220.    function outPipeMiss()
  221.    {
  222.       if(sarbakan.sound.SoundManager.isSoundPlaying(this.oSquishSound))
  223.       {
  224.          sarbakan.sound.SoundManager.fade(this.oSquishSound,sarbakan.sound.SoundManager.FADE_VOLUME,0,20);
  225.       }
  226.       if(this.stateFinished())
  227.       {
  228.          this.setState("idle");
  229.       }
  230.    }
  231.    function scared()
  232.    {
  233.       if(this.mc.state._currentframe == characters.SpongeBob.FR_SCARED_YELL)
  234.       {
  235.          sarbakan.sound.SoundManager.play("scared" + sarbakan.utils.MoreMath.random(1,2),maze.MazeData.VOLUME_SCARED,1);
  236.       }
  237.       if(this.stateFinished())
  238.       {
  239.          this.setState("idle");
  240.       }
  241.    }
  242.    function captured()
  243.    {
  244.       this.nSpeedX = 0;
  245.    }
  246.    function capturedPush()
  247.    {
  248.       if(!this.bBashingA || !this.bBashingB)
  249.       {
  250.          if(this.oCurrentSkeletuna.mc.state._currentFrame > 1)
  251.          {
  252.             this.oCurrentSkeletuna.mc.state.prevFrame();
  253.          }
  254.          else
  255.          {
  256.             this.oCurrentSkeletuna.mc.state.stop();
  257.          }
  258.       }
  259.       else
  260.       {
  261.          this.oCurrentSkeletuna.mc.state.nextFrame();
  262.          if(this.oCurrentSkeletuna.mc.state._currentFrame == this.oCurrentSkeletuna.mc.state._totalframes)
  263.          {
  264.             this.oCurrentSkeletuna.setState("attackEnd");
  265.             this.endBashingSkeletuna();
  266.             this.setState("idle");
  267.             this.mc._x = this.oCurrentSkeletuna.mc._x;
  268.          }
  269.       }
  270.       this.oCurrentSkeletuna.checkDry();
  271.       if(!sarbakan.sound.SoundManager.isSoundPlaying(this.oSquishSound))
  272.       {
  273.          this.oSquishSound = sarbakan.sound.SoundManager.play("skeletunaOut" + sarbakan.utils.MoreMath.random(1,3),maze.MazeData.VOLUME_SKELETUNAOUT,1);
  274.       }
  275.    }
  276.    function dry()
  277.    {
  278.       this.nSpeedX = 0;
  279.       if(this.stateFinished() && !this.bLevelRestarting)
  280.       {
  281.          this.bLevelRestarting = true;
  282.          _global.C.oWater.setState("done");
  283.          _global.C.oHUD.loseLife();
  284.       }
  285.    }
  286.    function putMask()
  287.    {
  288.       if(this.stateCurrentFrame() == 3)
  289.       {
  290.          sarbakan.sound.SoundManager.play("putMask",maze.MazeData.VOLUME_PUTMASK,1);
  291.       }
  292.       if(this.stateFinished())
  293.       {
  294.          this.setState("idle");
  295.          this.startMask();
  296.       }
  297.    }
  298.    function greaseSelf()
  299.    {
  300.       if(this.stateCurrentFrame() == 26)
  301.       {
  302.          sarbakan.sound.SoundManager.play("greaseA",maze.MazeData.VOLUME_GREASE,1);
  303.       }
  304.       if(this.stateFinished())
  305.       {
  306.          this.setState("idle");
  307.          this.startGrease();
  308.       }
  309.    }
  310.    function checkFall()
  311.    {
  312.       if(!this.isOnGround())
  313.       {
  314.          this.setState("fallStart");
  315.       }
  316.    }
  317.    function checkWall()
  318.    {
  319.       if(this.isHittingWall())
  320.       {
  321.          this.setPositionOutWall();
  322.          this.nSpeedX = 0;
  323.       }
  324.    }
  325.    function checkPipe()
  326.    {
  327.       if(this.oCurrentPipe && this.oKey.DOWN)
  328.       {
  329.          this.setState("jumpInPipe");
  330.          this.endMask();
  331.          this.oSlidingPipe = this.oCurrentPipe;
  332.          this.nSlideDistanceTotal = this.oCurrentPipe.mc._height;
  333.       }
  334.    }
  335.    function checkDry()
  336.    {
  337.       if(this.collideWithElement("water",this.mc.dry))
  338.       {
  339.          if(this.sState == "getInPipe")
  340.          {
  341.             this.endBashingPipe(false);
  342.          }
  343.          else
  344.          {
  345.             this.setState("dry");
  346.          }
  347.       }
  348.    }
  349.    function isOnGround()
  350.    {
  351.       return this.collideWithMap("floor",this.mc.bottom);
  352.    }
  353.    function isHittingWall()
  354.    {
  355.       return this.collideWithMap("floor",this.mc.front);
  356.    }
  357.    function canBeScared()
  358.    {
  359.       return !this.bMasked && (this.sState == "idle" || this.sState == "run");
  360.    }
  361.    function checkItem()
  362.    {
  363.       if(this.oKey.SPACE && _global.C.oHUD.getItem())
  364.       {
  365.          var _loc3_ = _global.C.oHUD.getItem();
  366.          _global.C.oHUD.useItem();
  367.          switch(_loc3_)
  368.          {
  369.             case maze.MazeData.ELEMENT_PUMPKIN:
  370.                this.endGrease();
  371.                this.setState("putMask");
  372.                break;
  373.             case maze.MazeData.ELEMENT_SLUG:
  374.                this.endMask();
  375.                this.setState("greaseSelf");
  376.          }
  377.          this.nSpeedX = 0;
  378.       }
  379.    }
  380.    function setPositionOutFloor()
  381.    {
  382.       while(this.isOnGround())
  383.       {
  384.          this.mc._y--;
  385.       }
  386.       this.mc._y = this.mc._y + 1;
  387.    }
  388.    function setPositionOutWall()
  389.    {
  390.       this.mc._x -= this.nSpeedX;
  391.    }
  392.    function startBashingPipe()
  393.    {
  394.       if(this.bGreased || this.bAutoBash)
  395.       {
  396.          this.oKeyBasherA = new sarbakan.input.KeyBasher(40,1,1,sarbakan.utils.Delegate.create(this,this.onBashReportPipe));
  397.          this.oKeyBasherA.enableAutoBashing();
  398.       }
  399.       else
  400.       {
  401.          this.nSlideModifier = Math.round(this.nSlideDistanceTotal / maze.MazeData.VIEW_TILE_HEIGHT);
  402.          if(this.nSlideModifier > 4)
  403.          {
  404.             this.nSlideModifier = 4;
  405.          }
  406.          this.oKeyBasherA = new sarbakan.input.KeyBasher(40,characters.SpongeBob.BASH_BASE_RATE * this.nSlideModifier,characters.SpongeBob.BASH_BASE_SPEED[_global.C.nDifficultyLevel] * this.nSlideModifier,sarbakan.utils.Delegate.create(this,this.onBashReportPipe));
  407.       }
  408.       this.bBashingA = true;
  409.    }
  410.    function startBashingSkeletuna()
  411.    {
  412.       this.oKeyBasherA = new sarbakan.input.KeyBasher(37,characters.SpongeBob.BASH_BASE_RATE,characters.SpongeBob.BASH_BASE_SPEED[_global.C.nDifficultyLevel],sarbakan.utils.Delegate.create(this,this.onBashReportSkeletuna,"bBashingA"));
  413.       this.oKeyBasherB = new sarbakan.input.KeyBasher(39,characters.SpongeBob.BASH_BASE_RATE,characters.SpongeBob.BASH_BASE_SPEED[_global.C.nDifficultyLevel],sarbakan.utils.Delegate.create(this,this.onBashReportSkeletuna,"bBashingB"));
  414.       this.bBashingA = false;
  415.       this.bBashingB = false;
  416.    }
  417.    function onBashReportPipe(l_bSuccess)
  418.    {
  419.       if(!sarbakan.visual.PauseManager.bPaused)
  420.       {
  421.          if(l_bSuccess)
  422.          {
  423.             this.mc.state.nextFrame();
  424.             if(this.stateFinished())
  425.             {
  426.                this.endBashingPipe(true);
  427.             }
  428.             this.bBashingA = true;
  429.          }
  430.          else
  431.          {
  432.             if(this.mc.state._currentframe == 1)
  433.             {
  434.                this.endBashingPipe(false);
  435.             }
  436.             this.bBashingA = false;
  437.          }
  438.       }
  439.    }
  440.    function endBashingPipe(l_bSuccess)
  441.    {
  442.       this.oKeyBasherA.end();
  443.       if(l_bSuccess)
  444.       {
  445.          this.startSlide();
  446.       }
  447.       else
  448.       {
  449.          this.setState("outPipeMiss");
  450.       }
  451.    }
  452.    function onBashReportSkeletuna(l_sVarName, l_bSuccess)
  453.    {
  454.       if(!sarbakan.visual.PauseManager.bPaused)
  455.       {
  456.          if(l_bSuccess)
  457.          {
  458.             this[l_sVarName] = true;
  459.          }
  460.          else
  461.          {
  462.             this[l_sVarName] = false;
  463.          }
  464.       }
  465.    }
  466.    function endBashingSkeletuna()
  467.    {
  468.       this.oKeyBasherA.end();
  469.       this.oKeyBasherB.end();
  470.    }
  471.    function startSlide()
  472.    {
  473.       this.nSpeedY = characters.SpongeBob.SPEED_SLIDE;
  474.       if(this.bGreased || this.bAutoBash || this.nSlideModifier >= 3)
  475.       {
  476.          this.nSpeedY = characters.SpongeBob.SPEED_SLIDE_GREASED;
  477.       }
  478.       this.nSlideDistance = 0;
  479.       this.setState("slidePipe");
  480.    }
  481.    function startMask()
  482.    {
  483.       this.bMasked = true;
  484.       this.nTimeout = 0;
  485.    }
  486.    function endMask()
  487.    {
  488.       if(this.bMasked)
  489.       {
  490.          this.nMaskDestroyCount = 0;
  491.          this.mcPumpkin.gotoAndPlay("remove");
  492.          this.bMaskDestroying = true;
  493.          sarbakan.sound.SoundManager.play("endMask",maze.MazeData.VOLUME_ENDMASK,1);
  494.       }
  495.    }
  496.    function updateMask()
  497.    {
  498.       if(this.bMasked || this.sState == "putMask")
  499.       {
  500.          this.mcPumpkin._visible = true;
  501.          if(this.bMaskDestroying)
  502.          {
  503.             if(++this.nMaskDestroyCount < characters.SpongeBob.FR_MASKDESTROY)
  504.             {
  505.                this.mcPumpkin.gotoAndStop("remove");
  506.                this.mcPumpkin.state.gotoAndStop(this.nMaskDestroyCount);
  507.             }
  508.             else
  509.             {
  510.                this.bMaskDestroying = false;
  511.                this.bMasked = false;
  512.                this.mcPumpkin._visible = false;
  513.             }
  514.          }
  515.       }
  516.       else
  517.       {
  518.          this.mcPumpkin._visible = false;
  519.       }
  520.       if(++this.nTimeout >= characters.SpongeBob.TIMEOUT_MASK && this.bMasked && !this.bMaskDestroying)
  521.       {
  522.          this.endMask();
  523.       }
  524.    }
  525.    function startGrease()
  526.    {
  527.       this.bGreased = true;
  528.       this.nTimeout = 0;
  529.    }
  530.    function endGrease()
  531.    {
  532.       this.bGreased = false;
  533.    }
  534.    function updateGrease()
  535.    {
  536.       if(this.bGreased)
  537.       {
  538.          this.mcGrease._visible = true;
  539.          if(++this.nTimeout >= characters.SpongeBob.TIMEOUT_GREASE)
  540.          {
  541.             this.endGrease();
  542.          }
  543.       }
  544.       else
  545.       {
  546.          this.mcGrease._visible = false;
  547.       }
  548.    }
  549. }
  550.